using System;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;
using PHSRAG.Insight21.Common.Pages;
using PHSRAG.Insight21.Policy;
using PHSRAG.Utility;

namespace PHSRAG.Insight21.Admin
{
    /// <summary>
    /// Conntains logic to display details about a specified HR department. 
    /// </summary>
    public partial class AdminHRDepartmentProfile : BasePage
    {
        #region Private Variables

        private ICachePolicy cachePolicy;

        #endregion

        #region Event Handlers

        /// <summary>
        /// Adds the page to the breadcrumb. Also loads the datagrid with HR departments if there are any in
        /// the cache.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">Information about the event itself.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            breadCrumb.Add("HR Department Profile",
                "AdminHRDepartmentProfile.aspx",
                Settings.GetKeyValue("BreadCrumb.DetailPageLevel", 2));

            if (!IsPostBack)
            {
                Hashtable incomingData = (Hashtable) Session[Keys.IncomingDataKey];
                DataSet hrDepartment = GetHRDepartmentDataSet(Convert.ToInt32(incomingData[Keys.HRDepartmentIdKey]));
                LoadDataControls(hrDepartment);
            }
        }

        /// <summary>
        /// Formats the names of HR department members to LastName, FirstName format.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">Information about the event itself.</param>
        protected void OnItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                DataRowView view = (DataRowView) e.Item.DataItem;
                ((Label) e.Item.FindControl("memberName")).Text = string.Format("{0}, {1}", view["LastName"], view["FirstName"]);
            }
        }

        /// <summary>
        /// Resets the subnavigation selected link before the 
        /// header performs a redirect.
        /// </summary>
        /// <param name="sender">Control which rasied the event.</param>
        /// <param name="e">Arguments associated with the event.</param>
        protected void OnHeaderNavigate(object sender, CommandEventArgs e)
        {
            adminSubNavigation.Reset();
        }

        #endregion

        #region Private Methods

        private DataSet GetHRDepartmentDataSet(int hrDepartmentID)
        {
            HRDepartment hrDepartment = new HRDepartment(cachePolicy);
            hrDepartment.Load(DataAccess.GetConnectionString(), hrDepartmentID);

            return hrDepartment.GetDataContainer();
        }

        /// <summary>
        /// Loads page controls with data. It should be noted that HRDepartments with 
        /// no management status (null ManagementStatus column value) 
        /// information are considered "UnManaged". 
        /// </summary>
        /// <param name="hrDepartment">Dataset containing HR Department info.</param>
        private void LoadDataControls(DataSet hrDepartment)
        {
            DataRow basicInfo = hrDepartment.Tables[0].Rows[0];
            hrDeptCode.Text = (string) basicInfo["HRDepartmentCode"];
            hrDeptName.Text = (string) basicInfo["HRDepartmentName"];
            hrDeptMgmtStatus.Text = basicInfo["ManagementStatus"] == DBNull.Value ? "Unmanaged" : (string) basicInfo["ManagementStatus"];

            // members
            departmentMembers.DataSource = hrDepartment.Tables[1];
            departmentMembers.DataBind();
            if (departmentMembers.Items.Count == 0)
                annunciator.Message = "The department has no members.";
        }

        #endregion

        #region Web Form Designer generated code

        /// <summary>
        /// Wires up page events.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);

            cachePolicy = (Policy.ICachePolicy) Session[Policy.Names.CachePolicyKey];
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
        }

        #endregion
    }
}